package org.vector.view;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import org.vector.test.Test;
import org.vector.utils.BaseDao;

@SuppressWarnings("serial")
public class ChangeUser extends JFrame{
	BaseDao bd = new BaseDao();
	private JTextField t1,t2,t3;
	private JButton btn,btn2;
	public ChangeUser() {

		setSize(300, 400);
		setLocationRelativeTo(null);
		setLayout(new FlowLayout());
		add(new JLabel("你的姓名是"));
		t2 = new JTextField(13);
		add(t2);
		add(new JLabel("你的手机号是"));
		t1 = new JTextField(13);
		add(t1);
		add(new JLabel("若修改密码需要填写"));
		t3 = new JTextField(13);
		add(t3);
		
		btn = new JButton("忘记密码");
		add(btn);
		
		btn2 = new JButton("修改密码");
		add(btn2);
		
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String a = t1.getText();
				int c = Integer.valueOf(a);
				String name = t2.getText();
				try {
					Class.forName("com.mysql.cj.jdbc.Driver");
					String url = "jdbc:mysql://localhost:3306/bank?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
					String user = "root";
					String password = "root";
					Connection connection = DriverManager.getConnection(url,user,password);
					String sql = "select * from bank where phone=? and name = ?";
					PreparedStatement ps = connection.prepareStatement(sql);
					ps.setInt(1, c);
					ps.setString(2, name);
					ResultSet resultset = ps.executeQuery();
					if(resultset.next()) {
						int as = resultset.getInt("bankid");
						String ss = Integer.toString(as);
						new Wcuowu(ss);
						int cs = resultset.getInt("password");
						String ccs = Integer.toString(cs);
						new Wcuowu(",密码" + ccs);
					}else {
						//System.out.println("用户名或密码错误");
						new Wcuowu("未找到");
					}
//					while(resultset.next()) {
//						//System.out.println("登陆成功");
//						int as = resultset.getInt("bankid");
//						String ss = Integer.toString(as);
//						new Wcuowu(ss);
//					}
				} catch ( Exception e1) {
					e1.printStackTrace();
				}
				
				
			}
		});
		btn2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String a = t3.getText();
				int s = Integer.valueOf(a);
				String acc = t1.getText();
				int c = Integer.valueOf(acc);
				String name = t2.getText();
				
				String sql = "update bank set password = ? where name = ? and phone = ?";
				int rows = bd.executeUpdate(sql, s,name,c);
				if(rows == 1) {
					new Test();
					new Wcuowu("修改成功，新密码是" + s);
					ChangeUser.this.dispose();
				}else {
					new Wcuowu("修改失败");
				}
				
			}
		});
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
	}
//	public static void main(String[] args) {
//		new ChangeUser();
//	}
}
